package lanou.autohome.my;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import lanou.autohome.R;
import lanou.autohome.basetool.BaseActivity;

/**
 * Created by dllo on 16/8/30.
 * 我的 -> 注册
 */
public class RegisterActivity extends BaseActivity{

    private EditText number;
    private EditText password;
    private String name;
    private String passwordEt;

    @Override
    protected int setLayout() {
        return R.layout.activity_register_me;
    }

    @Override
    protected void intiView() {
        //账号
        number = bindView(R.id.number_register_me);
        //密码
        password = bindView(R.id.password_register_me);
        //新建一个可以添加属性的文本对象
        SpannableString ss = new SpannableString("手机号/邮箱/用户名");
        SpannableString sss = new SpannableString("请输入您的账号密码");
        //新建一个属性对象 , 设置文字的大小
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(12, true);
        AbsoluteSizeSpan asss = new AbsoluteSizeSpan(12, true);
        //附加属性到文本
        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        sss.setSpan(asss, 0, sss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置hint , 一定要进行转换 , 否则属性会消失
        number.setHint(new SpannableString(ss));
        password.setHint(new SpannableString(sss));

        //注册按钮
        RelativeLayout zhuce = bindView(R.id.relativelayout_register_me);
        zhuce.setOnClickListener(this);

        ImageView back = bindView(R.id.imageview_register_back);
        back.setOnClickListener(this);

    }

    @Override
    protected void intiData() {
    }

    @Override
    public void onClick(View v){
        switch (v.getId()){
            //注册按钮
            case R.id.relativelayout_register_me:

                name = number.getText().toString();
                passwordEt = password.getText().toString();
                //注册用户
                BmobUser bmobUser = new BmobUser();
                bmobUser.setUsername(name);
                bmobUser.setPassword(passwordEt);
                bmobUser.signUp(new SaveListener<MyBemoUser>() {
                    @Override
                    public void done(MyBemoUser myBemoUser, BmobException e) {
                        if (e == null){
                            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            Intent register = new Intent();
                            register.putExtra("back" , true);
                            register.putExtra("用户名" , number.getText().toString());
                            register.putExtra("密码" , password.getText().toString());
                            setResult(2 , register);
                            finish();
                        }else {
                            Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                            Log.d("注册失败", "e:" + e);
                        }
                    }
                });

                break;
            case R.id.imageview_register_back:
                Intent reguster = new Intent();
                setResult(2 , reguster);
                finish();
                break;
        }
    }
}
